<template>
  <div class="app-container calendar-list-container">

    <div class="filter-container" style="padding-bottom: 10px;display: flex"><!-- 上边的搜索条件 -->
      <el-input
        @keyup.enter.native="handleFilter"
        style="width: 200px;"
        class="filter-item"
        placeholder="创建类型" v-model="listQuery.approve_step">
      </el-input>
      <el-input
        @keyup.enter.native="handleFilter"
        style="width: 200px;"
        class="filter-item"
        placeholder="物料名称" v-model="listQuery.approve_step">
      </el-input>
      <el-button class="filter-item" type="primary" icon="search" @click="handleFilter">搜索</el-button>
      <!--<el-button class="filter-item" type="primary" icon="document" @click="handleDownload">导出</el-button>-->
    </div>

    <!-- 展示数据的table -->
    <el-table
      :data="list"
      v-loading.body="listLoading"
      element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%">

      <el-table-column align="center" label="BOM_id" max-width="250px">
        <template scope="scope">
          <span>{{scope.row.bom_id}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="采购数量" max-width="250px">
        <template scope="scope">
          <span>{{scope.row.buy_qty}}</span>
        </template>
      </el-table-column>

      <el-table-column max-width="250px" align="center" label="币种">
        <template scope="scope">
          <span>{{scope.row.coin}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" max-width="250px">
        <template scope="scope">
          <el-button type="primary"
                     @click='toEdit(scope.row.require_id)'
                     size="small" icon="edit">编辑</el-button>
          <el-button
            size="small"
            type="danger"
            @click="delRow(scope.row.require_id)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <div v-show="!listLoading" class="pagination-container" style="margin-top: 30px;">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.curr_page"
                     :page-sizes="[1, 2, 3, 5]" :page-size="listQuery.page_size" layout="total, sizes, prev, pager, next, jumper" :total="total_count">
      </el-pagination>
    </div>

  </div>
</template>

<script>
  import { mrpList,mrpDel } from '@/api/purchase/requirePlan'

  export default {
    name: 'reqList',
    data() {
      return {
        list: null,
        listLoading: true,
        total_count:null,//数据总条数
        total_page:null,//总页数
        listQuery: {
          client_type:1,
          company_id:'',
          curr_page: 1,
          page_size: 2
        },
        delData:{
          client_type:1,
          require_id:''
        },
      }
    },
    mounted() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        mrpList(this.listQuery)
          .then(res => {
            this.list = res.require_list
            this.total_count = res.total_count
            this.total_page = res.total_page
            this.listLoading = false
          })
          .catch(err => {
            console.log(err)
          })
      },
      handleFilter() {
        this.listQuery.curr_page = 1
        this.getList()
      },
      handleSizeChange(val) {
        this.listQuery.page_size = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.curr_page = val
        this.getList()
      },
      delRow(id){
        this.$confirm('此操作将删除该条数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delData.require_id = id
          mrpDel(this.delData)
            .then(res => {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.listQuery.curr_page = 1
              this.getList()
            })
            .catch(err => {
              this.$message.error('删除失败!');
              console.log(err);
            })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      toEdit(id){
        this.$router.push({path:'createRequest',query:{require_id:id}})
      },
    }
  }
</script>

<style lang="scss" scoped>

</style>
